﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPool : MonoBehaviour {
    public static ObjectPool SharedInstance;
    public int PoolSize = 20;
    public GameObject PooledObject;//需要对象池的游戏物体
    private List<GameObject> Pool = new List<GameObject>();//池子

    private void Awake()
    {
        SharedInstance = this;
    }
    // Use this for initialization
    void Start () {
		for(int i = 0; i < PoolSize; i++)
        {
            GameObject tmp = Instantiate(PooledObject);
            //隐藏
            tmp.SetActive(false);
            Pool.Add(tmp);
        }
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    /// <summary>
    /// 从池中取1个
    /// </summary>
    /// <returns></returns>
    public GameObject GetPooledObject()
    {
        for(int i = 0; i < PoolSize; i++)
        {
            if (!Pool[i].activeInHierarchy)
            {
                Pool[i].SetActive(true);
                return Pool[i];
            }
        }
        return null;
    }
}
